package com.example.gametestproject;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Paint.Style;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.Shader.TileMode;
import android.graphics.Typeface;

import com.xxxx.game.*;

public class TextSprite extends SpriteBasic
{
	public enum HorizontalAlignment
	{
		AlignLeft,
		AlignCenter,
		AlginRight,
	}
	
	public enum VerticalAlignment
	{
		AlignTop,
		AlignCenter,
		AliginBottom
	}
	
	private String mTextContent;
	private float mTextSize;
	
	private float mOuterStroke = 5;
	private float mInnerStroke = 3;
	private int mOuterColor = Color.GREEN;
	private int mInnerColor1 = Color.WHITE;
	private int mInnerColor2 = Color.RED;
	private int mAlpha = 255;
	
	private HorizontalAlignment mHorzAlignmentType;
	private VerticalAlignment mVertAlignmentType;
	
	public TextSprite(String txt, int textSize, 
			int outerColor, int innerColor1, int innerColor2,
			float outerStroke, float innerStroke, 
			HorizontalAlignment halignment, VerticalAlignment valignment)
	{
		mTextContent = txt;
		mTextSize = textSize;
		mOuterStroke = outerStroke;
		mInnerStroke = innerStroke;
		mOuterColor = outerColor;
		mInnerColor1 = innerColor1;
		mInnerColor2 = innerColor2;
		mHorzAlignmentType = halignment;
		mVertAlignmentType = valignment;
		
		this.setDrawFunc(mTextDrawing);
	}
	
	@Override
	public boolean isLiving()
	{
		return true;
	}
	
	public float getTextSize()
	{
		return mTextSize;
	}
	
	public void setTextSize(float textsize)
	{
		mTextSize = textsize;
	}
	
	public float getOuterStroke()
	{
		return this.mOuterStroke;
	}
	
	public float getInnerStroke()
	{
		return this.mInnerStroke;
	}
	
	public void setOuterStroke(float outerStroke)
	{
		this.mOuterStroke = outerStroke;
	}
	
	public void setInnerStroke(float innerStroke)
	{
		this.mInnerStroke = innerStroke;
	}
	
	public void setTextContent(String text)
	{
		this.mTextContent = text;
	}
	
	public String getTextContent()
	{
		return this.mTextContent;
	}
	
	public void setInnerColor1(int clr)
	{
		this.mInnerColor1 = clr;
	}
	
	public void setInnerColor2(int clr)
	{
		this.mInnerColor2 = clr;
	}
	
	public void setOuterColor(int clr)
	{
		this.mOuterColor = clr;
	}
	
	public int getInnerColor1()
	{
		return this.mInnerColor1;
	}
	
	public int getInnerColor2()
	{
		return this.mInnerColor2;
	}
	
	public int getOuterColor()
	{
		return this.mOuterColor;
	}
	
	public int getAlpha()
	{
		return this.mAlpha;
	}
	
	public void setAlpha(int alpha)
	{
		this.mAlpha = Math.max(0, Math.min(255, alpha));
	}
	
	@Override
	public SizeF getSize()
	{
		SizeF size = new SizeF();
		
		Paint paint = this.getPaint();
		paint.setAntiAlias(true);
		paint.setTextSize(this.mTextSize);
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeWidth(this.mOuterStroke);
		paint.setShader(null);
		FontMetrics fm = paint.getFontMetrics();
		size.Height = (float) Math.ceil(fm.descent - fm.top);
		size.Width = paint.measureText(this.mTextContent);
		return size;
	}
	
	private ICanvasDrawing mTextDrawing = new ICanvasDrawing()
	{
		@Override
		public boolean paint(Canvas canvas, ISprite sprite)
		{
			TextSprite tsprite = (TextSprite)sprite;
			Paint paint = tsprite.getPaint();
			paint.setColor(mOuterColor);
			paint.setAntiAlias(true);
			paint.setTextSize(tsprite.mTextSize);
			paint.setStyle(Style.FILL_AND_STROKE);
			//paint.setTypeface(Typeface.DEFAULT_BOLD);
			paint.setStyle(Paint.Style.STROKE);
			paint.setStrokeWidth(tsprite.mOuterStroke);
			paint.setShadowLayer(3, 3, 3, (tsprite.mOuterColor & 0xFF000000) | 0x00FF00FF);
			paint.setShader(null);
			Rect outerRect = new Rect();
			paint.getTextBounds(tsprite.mTextContent, 0, tsprite.mTextContent.length(), outerRect);
			
			PointF ptOuter = this.getDrawPosition(tsprite.getPosition(), outerRect.height(), outerRect.width(), 0, 0, 
					tsprite.mHorzAlignmentType, tsprite.mVertAlignmentType);
			
			//paint.setAlpha(tsprite.getAlpha());
			canvas.drawText(tsprite.mTextContent, ptOuter.x, ptOuter.y - outerRect.top, paint);
			
			//paint.setColor(tsprite.mInnerColor1);
			paint.setAntiAlias(true);
			paint.setStyle(Style.FILL_AND_STROKE);
			paint.setStrokeWidth(tsprite.mInnerStroke);
			paint.setTypeface(Typeface.DEFAULT);
			
			Rect innerRect = new Rect();
			paint.getTextBounds(tsprite.mTextContent, 0, tsprite.mTextContent.length(), innerRect);
			
			PointF ptInner = this.getDrawPosition(tsprite.getPosition(), innerRect.height(), innerRect.width(), 
					outerRect.width() - innerRect.width(), outerRect.height() - innerRect.height(), 
					tsprite.mHorzAlignmentType, tsprite.mVertAlignmentType);
			Shader shader = new LinearGradient(0, ptInner.y, 0, ptInner.y + innerRect.height(), 
					tsprite.mInnerColor1, tsprite.mInnerColor2, TileMode.CLAMP);
			paint.setShader(shader);
			paint.setShadowLayer(0, 0, 0, 0);
			//paint.setAlpha(tsprite.getAlpha());
			canvas.drawText(tsprite.mTextContent, ptInner.x, ptInner.y - innerRect.top, paint);
			
			return tsprite.isLiving();
		}
		
		private PointF getDrawPosition(PointF pt, float txtHeight, float txtWidth, float dx, float dy,
				HorizontalAlignment horzalign, VerticalAlignment vertalign)
		{
			PointF ptText = new PointF(0, 0);
			if (horzalign == HorizontalAlignment.AlignLeft)
			{
				ptText.x = pt.x + dx / 2;
			}
			else if (mHorzAlignmentType == HorizontalAlignment.AlignCenter)
			{
				ptText.x = pt.x - txtWidth / 2;
			}
			else
			{
				ptText.x = pt.x - txtWidth - dx / 2;
			}
			
			if (vertalign == VerticalAlignment.AlignTop)
			{
				ptText.y = pt.y + dy / 2;
			}
			else if (vertalign == VerticalAlignment.AlignCenter)
			{
				ptText.y = pt.y - txtHeight / 2;
			}
			else
			{
				ptText.y = pt.y - txtHeight - dy / 2;
			}
			return ptText;
		}

		@Override
		public String getName()
		{
			return "TextDrawing";
		}
	};

	@Override
	public void makedead()
	{
		// do nothing
	}
}
